<template>
	<view class="container">
		<Header>调研考察</Header>
		<view class="content">
			<view class="title">
				{{data.kyname}}
			</view>
			<view class="item">
				<view class="item-content">
					<view v-html="data.content"></view>
				</view>
			</view>
			<!-- <view class="btn attend" v-if="!data.status" @click="btnClick(1)">
				确认参加
			</view>
			<view class="btn leave" v-if="!data.status" @click="btnClick(2)">
				我要请假
			</view> -->
			
			<view class="bottom">
				<view class="btn sign_in" :class="{'disabled': data.signStatus != 1}" @click="scanCode">扫码签到</view>
				<view class="btn leave" :class="{'disabled': data.signStatus != 1}" @click="askLeave">我要请假</view>
				<view class="btn report" @click="toPage()">活动报告</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Header from '../../components/header.vue'
	import request from '../../common/request.js'
	export default {
		components: {
			Header
		},
		data() {
			return {
				id: '',
				data:{}
			}
		},
		onLoad(option) {
			this.id  = option.id
		},
		mounted(){
			this.getDetailData()
		},
		methods: {
			toPage() {
				uni.navigateTo({
					url: `/pages/resumption/meetingReport?id=${this.id}`
				})
			},
			attendMetting() {
				let time = new Date().getTime()
				let startTime = new Date(this.data.start_time).getTime()
				let endTime = new Date(this.data.end_time).getTime()
				if (time > endTime) {
					uni.showModal({
						content: '会议已结束',
						showCancel: false,
						success: (res) => {
			
						}
					});
					return false
				}
				if (time > startTime) {
					uni.showModal({
						content: '会议已开始',
						showCancel: false,
						success: (res) => {
			
						}
					});
					return false
				}
				uni.showModal({
					content: '确认要参加会议吗？',
					success: (res) => {
						if (res.confirm) {
							this.signIn(this.id, 'attend')
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			getUrlParams(href, name) {
				var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
				if (reg.test(href)) {
					return unescape(RegExp.$2.replace(/\+/g, " "))
				};
				return "";
			},
			scanCode() {
				if(this.data.signStatus == 2){
					uni.showModal({
						content: '您已签到!',
						showCancel: false,
						success: (res) => {
					
						}
					});
					return false
				}else if(this.data.signStatus == 3){
					uni.showModal({
						content: '您已请假!',
						showCancel: false,
						success: (res) => {
					
						}
					});
					return false
				}
				uni.scanCode({
					success: (res) => {
						// console.log('条码类型：' + res.scanType);
						// console.log('条码内容：' + res.result);
						// uni.navigateTo({
						// 	url: res.result
						// })
						let id = this.getUrlParams(res.path, 'meet_id')
						let type = this.getUrlParams(res.path, 'codeType')
						signIn(id, type, this.data.address, () => {
							this.getDetailData()
						})
					}
				});
			},
			askLeave() {
				if(this.data.signStatus == 2){
					uni.showModal({
						content: '您已签到!',
						showCancel: false,
						success: (res) => {
					
						}
					});
					return false
				}else if(this.data.signStatus == 3){
					uni.showModal({
						content: '您已请假!',
						showCancel: false,
						success: (res) => {
					
						}
					});
					return false
				}
				uni.showModal({
					content: '确认要请假吗？',
					success: (res) => {
						if (res.confirm) {
							request({
								url: '/survey/askLeave',
								data: {
									meet_id: this.id,
									codeType: 'survey'
								},
								method: 'POST'
							}).then(res => {
								uni.showModal({
									content: res.msg,
									showCancel: false,
									success: function(res) {
			
									}
								});
								if (res.code == 1) {
									this.data.signStatus = 3
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			getDetailData(){
				request({
					url:'/survey/detail',
					data:{
						survey_id: this.id
					},
					method: 'POST'
				}).then(res => {
					if(res.code == 1){
						this.data = res.data
					}else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				})
			},
			btnClick(type){
				let time = new Date().getTime()
				let startTime = new Date(this.data.kystart).getTime()
				let endTime = new Date(this.data.kyend).getTime()
				if(time > endTime){
					uni.showModal({
						content: '调研考察已结束',
						showCancel: false,
						success: (res) => {
							
						}
					});
					return false
				}
				if(time > startTime){
					uni.showModal({
						content: '调研考察已开始',
						showCancel: false,
						success: (res) => {
							
						}
					});
					return false
				}
				request({
					url:'/survey/joinMake',
					data:{
						survey_id: this.id,
						status: type
					},
					method: 'POST'
				}).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					});
					if(res.code == 1){
						this.data.status = type
					}
				})
			}
		}
	}
</script>

<style  lang="scss" scoped>
	.content {
		padding: 20px;
		line-height: 26px;
	}

	.title {
		font-size: 16px;
		font-weight: bold;
		text-align: center;
		line-height: 26px;
		margin-bottom: 10px;
	}


	.item {
		padding: 5px 0px;
		font-size: 14px;
		/* border-bottom: 1px solid #e2e2e2; */
	}
	.flex{
		display: flex;
	}
	.item label{
		width: 100px;
		/* color: #ea5b6a; */
	}
	.item-content{
		margin-top: 8px;
	}
	// .btn {
	// 	color: #fff;
	// 	padding: 10px 20px;
	// 	border-radius: 30px;
	// 	margin: 20px 0px;
	// 	font-size: 16px;
	// 	text-align: center;
	// }
	
	// .attend {
	// 	background-color: #2668be;
	// }
	// .leave {
	// 	background-color: #fb7a30;
	// }
	
	.bottom {
		position: sticky;
		bottom: 0;
		padding: 20px 0px;
		background-color: #fff;
		width: 100%;
		text-align: center;
	
		.btn {
			color: #fff;
			// padding: 10px 20px;
			border-radius: 20px;
			margin: 0px 10px;
			font-size: 14px;
			text-align: center;
			max-width: 120px;
			width: calc(33.33% - 20px);
			height: 40px;
			line-height: 40px;
			display: inline-block;
		}
	
		.attend {
			background-color: #be3f26;
		}
	
		.sign_in {
			background-color: #09a884;
			&.disabled{
				background-color: rgba(9,168,132, 0.5);
			}
		}
	
		.leave {
			background-color: #fb7a30;
			&.disabled{
				background-color: rgba(251,122,48, 0.5);
			}
		}
	
	
		.report {
			background-color: #2668be;
		}
	
	}
</style>